/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.consultation;

import static controller.Connexion.CONF_DAO_INIT;
import static controller.administration.SaisieNotes.ATT_GESTION_COURS;
import dao.DAOInit;
import dao.interfaces.DAOCours;
import dao.interfaces.DAOEtudiant;
import dao.interfaces.DAOExamen;
import dao.interfaces.DAONote;
import gestionform.GestionCours;
import gestionform.GestionEtudiant;
import gestionform.GestionExamen;
import gestionform.GestionNote;
import gestionform.GestionRecapNotes;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Cour;
import model.Etudiant;
import model.Examen;
import model.Moyenne;
import model.Note;

/**
 *
 * @author Paul
 */
public class RecapNotes extends HttpServlet {

    public static final String VUE_RECAP_NOTES = "/WEB-INF/consultation/recapNotes.jsp";
    private DAOCours daoCours;
    private DAOExamen daoExamen;
    private DAOEtudiant daoEtudiant;
    private DAONote daoNote;

    @Override
    public void init() throws ServletException {
        /* Récupération d'une instance de notre DAO Personne */
        this.daoCours = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAOCours();
        this.daoExamen = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAOExamen();
        this.daoEtudiant = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAOEtudiant();
        this.daoNote = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAONote();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Etudiant e = (Etudiant) request.getSession().getAttribute("etudiant");
        if (e != null) {
            if (request.getSession().getAttribute("listeMoyennes") == null) {
                HttpSession session = request.getSession(true);
                GestionCours gCours = new GestionCours(daoCours);
                request.setAttribute(ATT_GESTION_COURS, gCours);
                ArrayList<Cour> listeCours = gCours.getListeCoursEtudiant(e.getId());
                GestionExamen gExamen = new GestionExamen(daoExamen);
                ArrayList<Examen> listeExamen = new ArrayList<Examen>();
                for (Cour cour : listeCours) {
                    listeExamen.addAll(gExamen.getExamens(cour.getIdCour()));
                }
                GestionNote gNote = new GestionNote(daoNote);
                ArrayList<Note> listeNotes = new ArrayList<Note>();
                for (Examen examen : listeExamen) {
                    listeNotes.add(gNote.getNoteEtudiantExamen(e.getId(), examen.getId()));
                }
                GestionEtudiant gEtudiant = new GestionEtudiant(daoEtudiant);
                GestionRecapNotes grn = new GestionRecapNotes(daoExamen, daoCours, daoNote);
                ArrayList<Moyenne> listeMoyenne = grn.affMoyennes(e);
                session.setAttribute("listeMoyennes", listeMoyenne);
                Float moyenneGeneral = gEtudiant.affMoyenneGeneral(listeMoyenne);
                session.setAttribute("moyGeneral", moyenneGeneral);
            }
            request.getRequestDispatcher(VUE_RECAP_NOTES).forward(request, response);

        } else {
            request.getRequestDispatcher("view/connexion/connexion.jsp").forward(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Etudiant e = (Etudiant) request.getSession().getAttribute("etudiant");
        if (e != null) {
            request.getRequestDispatcher(VUE_RECAP_NOTES).forward(request, response);
        } else {
            request.getRequestDispatcher("view/connexion/connexion.jsp").forward(request, response);
        }
    }
}
